/**
 *
 * Created by shenzaifang on 2019-03-30
 */
import {finishParam, releaseParam} from '../components/transaction/common';
import {pageTo} from 'nc-lightapp-front';

const contConfig = {
    sign: {
        // 合同签订
        pagecode: '60110303p',
        appcode: '60110303'
    },
    change: {
        //变更
        appcode: '60110305',
        pagecode: '60110305p'
    },
    extend: {
        //续签
        appcode: '60110307',
        pagecode: '60110307p'
    },
    terminate: {
        //解除
        appcode: '60110309',
        pagecode: '60110309p'
    },
    end: {
        //终止
        appcode: '60110311',
        pagecode: '60110311p'
    }
};

const proConfig = {
    signPro: {
        //协议签订
        appcode: '60110501',
        pagecode: '60110501p'
    },
    changePro: {
        //协议变更
        appcode: '60110503',
        pagecode: '60110503p'
    },
    releasePro: {
        //协议解除
        appcode: '60110505',
        pagecode: '60110505p'
    },
    finishPro: {
        //协议终止
        appcode: '60110507',
        pagecode: '60110507p'
    }
};

/**
 * 跳转到其他节点
 * @param pk_psndoc_subs  {Array} 合同主键
 * @param hrorgobj {Object} 人力资源组织
 * @param pageUrl {String}  /nccloud/resources/hrcm/contractmgt/change/main/index.html
 */
export function jump(pk_psndoc_subs, hrorgobj, pageUrl) {
    let pagesConfig;
    let pageQueryParam = window.localStorage.getItem('pageQueryParam');
    pageQueryParam = pageQueryParam ? JSON.parse(pageQueryParam) : {};
    let isPro = false;
    if (pageUrl.indexOf('/contractmgt/') !== -1) {
        pagesConfig = contConfig;
    } else if (pageUrl.indexOf('/protocolmgt/') !== -1) {
        pagesConfig = proConfig;
        isPro = true;
    }
    let pageUrlArr = pageUrl.match(/(contractmgt|protocolmgt)\/(\S*)\/main/);
    let key = pageUrlArr && pageUrlArr[2];
    if (key) {
        if (key === 'terminate') {
            pageQueryParam[key] = {
                pk_psndoc_subs,
                ...releaseParam,
                hrorgobj
            };
        } else if (key === 'end') {
            pageQueryParam[key] = {
                pk_psndoc_subs,
                ...finishParam,
                hrorgobj
            };
        } else if (key === 'sign') {
            key = isPro ? key + "Pro" : key;
            pageQueryParam[key] = {
                pk_psndoc_subs,
                hrorgobj
            };
        } else {
            pageQueryParam[key] = {
                pk_psndoc_subs,
                ...releaseParam,
                ...finishParam,
                hrorgobj
            };
        }
    }
    let param = pagesConfig[key];

    // 保存数据到本地
    window.localStorage.setItem('pageQueryParam', JSON.stringify(pageQueryParam));
    try {
        pageTo.openTo(pageUrl, param);
    } catch (e) {
        let localUrl = pageUrl.split('nccloud/resources')[1];
        let tempwindow = window.open();
        tempwindow.location = `${localUrl}#/?c=${pageQueryParam[key].appcode}&p=${pageQueryParam[key].pagecode}`;
    }
}

export function getAllTreeRefpk(props, id) {
    let refpk = [];
    let allData = props.asyncTree.getAsyncTreeValue(id);

    function getrefpk(data) {
        if (Array.isArray(data)) {
            data.forEach((item) => {
                getrefpk(item);
            });
        } else {
            if (data.hasOwnProperty('refpk')) {
                refpk.push(data.refpk);
            }
            if (data.hasOwnProperty('children')) {
                getrefpk(data.children);
            }
        }
    }

    getrefpk(allData);
    return refpk;
}

export let nodeRefs = {wpsview: null}